/**
 * 环境要求
 * 最新版 nodejs
 * 全局安装
 * npm install gulp -g
 * 目录安装
 * npm install gulp --save-dev
 * 组件安装
 * gulp-minify-css gulp-minify-html gulp-uglify gulp-concat --save-dev
 */

// gulp-autoprefixer  css3前缀补全

var gulp = require('gulp'),//加载gulp
	watch  = require('gulp-watch'),
	order = require('gulp-order'),
    htmlmin = require('gulp-minify-html'),//html压缩
    minify = require('gulp-minify-css'),//css压缩
    uglify = require('gulp-uglify'),//js压缩
    imagemin = require('gulp-imagemin'),//图片压缩
    concat = require("gulp-concat"),//文件合并
    livereload = require('gulp-livereload'),//自动刷新
    plumber = require('gulp-plumber');// plumber(当任务发生错误时, 阻止gulp自动退出)

//复制文件
gulp.task('copy', function(){
    gulp.src(["public/js/tool/**"])
        .pipe(gulp.dest("dist/js/tool"))
    gulp.src(['public/images/**'])
        .pipe(gulp.dest("dist/images"))
});

//按加载顺序压缩js
gulp.task('jsmin',function(){ 
    gulp.src("public/js/**/*.js")
        .pipe(plumber())
        .pipe(order([
            "common/setFontSize.js",
            "library/angular1.4.4.min.js",
            "library/angular-ui-router.min.js",
            "library/angular-local-storage.js",
            "library/angular-sanitize.min.js",
            "library/angular-animate.js",
            "library/bindonce.min.js",
            "library/angular-isscroll.js",
            "library/angular-touch.js",
            "library/goback.js",
            "common/config.js",
            "common/route.js",
            "common/commonfactory.js",
            "common/cachefactory.js",
            "common/plusnavite.js",
            "tool/layer/layer.m.js",
            "library/vg-src.js",
            "common/global.js",
            "main-controller.js",
            "product-controller.js",
            "current-controller.js",
            "safety-controller.js",
            "other-controller.js",
            "center-controller.js",
            "more-controller.js",
            "directive-controller.js"
        ])) 
    //   .pipe(concat("all.js"))
      .pipe(uglify())
      .pipe(gulp.dest("dist/js"))
  
});

//压缩css
gulp.task('cssmin', function () {
    gulp.src(['public/css/**'])
        .pipe(plumber())
    	.pipe(concat('all.css'))
        .pipe(minify())
        .pipe(gulp.dest('dist/css'))
});

//压缩html
gulp.task('htmlmin', function () {
    gulp.src('public/html/**')
        .pipe(plumber())
        .pipe(htmlmin())
        .pipe(gulp.dest('dist/html'))
        .pipe(livereload());
})

// 默认任务
gulp.task("default",["copy","jsmin",'cssmin','htmlmin']);

var watcher = gulp.watch([
    "public/**/*.js","public/html/**/*.html","public/css/*.css"
], ["copy","jsmin",'cssmin','htmlmin']);
watcher.on("change", function(ev) {
    console.log("File" + ev.path + "发生" + ev.type + ",执行任务");
})
